<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class AkadControllerSiam extends MX_Controller {

    private $_template = "siam_iainmataram";

    function __construct()
	{
		parent::__construct();
        $this->access_library_siam->is_logged_in();
        $this->load->model('akad_model');
        $this->load->model('sirak_akademik/sirak_akad_model');
	}

	function viewJadwalKuliah()
	{
        //$data['mhs'] = $this->akad_model->getDataMahasiswa($this->session->userdata('siamunp_user'));

        $data['kontrol'] = $this->system_model->getViewKontrolData($this->session->userdata('siamunp_user_prodi'));

        /*$id_hari = $this->uri->segment(3); */
        $data['hari'] = $this->system_model->getValueHari();

        if($data['hari'])
        {
            $cekTemporaryEnrollmentsAdaYangBelumValidasi = $this->akad_model->cekTemporaryEnrollments($this->session->userdata('siamunp_user'),$this->session->userdata('system_tahun_ajaran'),$this->session->userdata('system_semester'));

            if($cekTemporaryEnrollmentsAdaYangBelumValidasi == TRUE) // ada yg belum divalidasi Ref: kprs
              $data['adaYangBelumDivalidasi'] = TRUE;
            else
              $data['adaYangBelumDivalidasi'] = FALSE;
        }

        /*if ( ($id_hari == NULL) || ($id_hari > 5) )*/
           /*$id_hari = 1; */

        //if ( ($data['mhs'] != NULL) && ($data['kontrol'] != NULL) )
           /*$data['jadwalKuliah'] = $this->akad_model->getJadwalKuliah($this->session->userdata('siamunp_user_prodi'), $data['kontrol']->KODE_SEMESTER, $data['kontrol']->KODE_TAHUN,$id_hari); */
        //else
           //$data['jadwalKuliah'] = NULL;

        $data['page'] = 'lihat_jadwal_kuliah';

        $data['atts'] = array(
                            'width'      => '900',
                            'height'     => '768',
                            'scrollbars' => 'yes',
                            'status'     => 'yes',
                            'resizable'  => 'yes',
                            'screenx'    => '0',
                            'screeny'    => '0'
                           );

        $this->load->view($this->_template,$data);
	}

    function viewJadwalUjian($request_day=0)
	{
        $data['kontrol'] = $this->system_model->getViewKontrolData($this->session->userdata('siamunp_user_prodi'));

        $day = array();
        $index_day = 0;
        $distinct_tgl_ujian = $this->akad_model->getDistinctJadwalUjian($data['kontrol']->KODE_SEMESTER,$data['kontrol']->KODE_TAHUN,$this->session->userdata('siamunp_user_prodi'));
        if($distinct_tgl_ujian){
          foreach($distinct_tgl_ujian as $tgl_ujian){
          $day[$index_day] = $tgl_ujian->TANGGAL_UJIAN;
          $index_day++;
          }
        }

        $request_day = ($request_day > $index_day-1)? '0' : $request_day;
        $data['max_day'] = $index_day;
        if($index_day != 0) $data['current_day'] = $day[$request_day];
        $data['current_request_day'] = $request_day;

        if($index_day == 0){
          $data['jadwalUjian'] = 0;
        }
        else{
          $data['jadwalUjian'] = $this->akad_model->getJadwalUjianByTgl($this->session->userdata('siamunp_user_prodi'), $data['kontrol']->KODE_SEMESTER, $data['kontrol']->KODE_TAHUN,$day[$request_day]);
        }

        //$data['jadwalUjian'] = $this->akad_model->getJadwalUjian($this->session->userdata('siamunp_user_prodi'), $data['kontrol']->KODE_SEMESTER, $data['kontrol']->KODE_TAHUN);

        $data['page'] = 'lihat_jadwal_ujian';

        $this->load->view($this->_template,$data);
	}

    function insertMkDitawarkan()
	{
        $mhs = $this->akad_model->getDataMahasiswa($this->session->userdata('siamunp_user'));

        if($mhs != NULL)
        {

           $data['kontrol'] = $this->system_model->getViewKontrolData($mhs->KODE_PRODI);

           $validasi = $this->akad_model->cekValidasi($this->session->userdata('siamunp_user'),$data['kontrol']->KODE_SEMESTER,$data['kontrol']->KODE_TAHUN);

           $mahasiswa = $this->akad_model->getMahasiswa($this->session->userdata('siamunp_user'));

           if($data['kontrol']->KODE_STATUS !=1)
           {
             $data['error'] = "Error: Saat ini diluar masa entri KRS.";
             $data['page'] = 'isi_krs_error';

             $this->load->view($this->_template,$data);
           }
           else if($mahasiswa->STATUS == 0)
           {
             $data['error'] = "Error: Anda tidak bisa melakukan pengisian KRS karena belum melakukan registrasi.";
             $data['page'] = 'isi_krs_error';

             $this->load->view($this->_template,$data);
           }
           else if($validasi==TRUE)
           {
             $data['error'] = "Error: Anda tidak bisa melakukan pengisian KRS karena KRS telah divalidasi oleh Dosen Pembimbing Akademik.";
             $data['page'] = 'isi_krs_error';

             $this->load->view($this->_template,$data);
           }
           else if(!$this->input->post('mk') AND !$this->input->post('nim') )
           {
             $data['getDataMkDitawarkan'] = $this->akad_model->getViewJadwalKuliah($data['kontrol']->KODE_TAHUN, $data['kontrol']->KODE_SEMESTER, $mhs->KODE_PRODI);

             $data['krs'] = $this->akad_model->getViewKrsTemp($this->session->userdata('siamunp_user'),$data['kontrol']->KODE_SEMESTER,$data['kontrol']->KODE_TAHUN);

             $data['kelompokkelas'] = $mhs->KELOMPOK_KELAS;

             $data['page'] = 'daftar_mk_ditawarkan';

             $this->load->view($this->_template,$data);

           }
           else if ( ($this->input->post('mk')) || ($this->input->post('nim')) )
           {
             $this->prosesEntriKrs();
           }

        }
        else if($mhs == NULL)
        {
          $data['kontrol'] = NULL;
          $data['error'] = "Error: Maaf, data tidak dapat ditampilkan. Mungkin ada gangguan teknis. Silakan menghubungi bagian terkait.";
          $data['page'] = 'isi_krs_error';

          $this->load->view($this->_template,$data);

        }

    }

    function prosesEntriKrs()
	{
        $mhs = $this->akad_model->getDataMahasiswa($this->session->userdata('siamunp_user'));

        if($mhs != NULL)
        {

          $data['kontrol'] = $this->system_model->getViewKontrolData($mhs->KODE_PRODI);

          $validasi = $this->akad_model->cekValidasi($this->session->userdata('siamunp_user'),$data['kontrol']->KODE_SEMESTER,$data['kontrol']->KODE_TAHUN);

          $mahasiswa = $this->akad_model->getMahasiswa($this->session->userdata('siamunp_user'));

          if($data['kontrol']->KODE_STATUS !=1)
          {
            $data['error'] = "Error: Saat ini diluar masa entri KRS.";
            $data['page'] = 'isi_krs_error';
            $this->load->view($this->_template,$data);
          }
          else if($mahasiswa->STATUS == 0)
          {
            $data['error'] = "Error: Anda tidak bisa melakukan pengisian KRS karena belum melakukan registrasi.";
            $data['page'] = 'isi_krs_error';
            $this->load->view($this->_template,$data);
          }
          else if($validasi==TRUE)
          {
             $data['error'] = "Error: Anda tidak bisa melakukan pengisian KRS karena KRS telah divalidasi oleh Dosen Pembimbing Akademik.";
             $data['page'] = 'isi_krs_error';

             $this->load->view($this->_template,$data);
          }
          else if($this->input->post('nim') == NULL)
          {
            if($data['kontrol']->KODE_SEMESTER ==1){

              $kode_semester = 2;

              $tahun = $data['kontrol']->KODE_TAHUN  - 1;
            }
            else if($data['kontrol']->KODE_SEMESTER ==2){

              $kode_semester = 1;

              $tahun = $data['kontrol']->KODE_TAHUN;
            }

            $data['getDataKhs'] = $this->akad_model->getViewKhsAll($this->session->userdata('siamunp_user'),$tahun,$kode_semester);

            $data['krs'] = $this->akad_model->getViewKrsTemp($this->session->userdata('siamunp_user'),$data['kontrol']->KODE_SEMESTER,$data['kontrol']->KODE_TAHUN);

            $data['matakuliahBaru'] = $this->akad_model->dataMatakuliahTersediaById($this->input->post('mk'));

            $data['page'] = 'proses_krs';
            $this->load->view($this->_template,$data);

          }
          else
          {
            if($this->input->post('tersedia') != "0000")
            {
              $this->akad_model->insertDataKrs($this->input->post('tersedia'), $this->input->post('nim'));
            }

            redirect('siam/entri-krs.html', 'refresh');
          }

        }
        else if($mhs == NULL)
        {
          $data['kontrol'] = NULL;
          $data['error'] = "Error: Maaf, data tidak dapat ditampilkan. Mungkin ada gangguan teknis. Silakan menghubungi bagian terkait.";
          $data['page'] = 'isi_krs_error';
          $this->load->view($this->_template,$data);
        }


	}

    function insertKrs()
	{
        $mhs = $this->akad_model->getDataMahasiswa($this->session->userdata('siamunp_user'));

        if($mhs != NULL)
        {

        $data['kontrol'] = $this->system_model->getViewKontrolData($mhs->KODE_PRODI);

        $validasi = $this->akad_model->cekValidasi($this->session->userdata('siamunp_user'),$data['kontrol']->KODE_SEMESTER,$data['kontrol']->KODE_TAHUN);

        $mahasiswa = $this->akad_model->getMahasiswa($this->session->userdata('siamunp_user'));

        if($data['kontrol']->KODE_STATUS !=1)
        {
          $data['error'] = "Error: Saat ini diluar masa entri KRS.";
          $data['page'] = 'isi_krs_error';
        }
        else if($mahasiswa->STATUS == 0)
        {
          $data['error'] = "Error: Anda tidak bisa melakukan pengisian KRS karena belum melakukan registrasi.";
          $data['page'] = 'isi_krs_error';
        }
        else if($validasi==TRUE)
        {
          $data['error'] = "Error: Anda tidak bisa melakukan pengisian KRS karena KRS telah divalidasi oleh Dosen Pembimbing Akademik.";
          $data['page'] = 'isi_krs_error';
        }
        else
        {
          if($data['kontrol']->KODE_SEMESTER ==1){

            $kode_semester = 2;

            $tahun = $data['kontrol']->KODE_TAHUN  - 1;
          }
          else if($data['kontrol']->KODE_SEMESTER ==2){

            $kode_semester = 1;

            $tahun = $data['kontrol']->KODE_TAHUN;
          }

          $data['getDataKhs'] = $this->akad_model->getViewKhsAll($this->session->userdata('siamunp_user'),$tahun,$kode_semester);

          $data['krs'] = $this->akad_model->getViewKrsTemp($this->session->userdata('siamunp_user'),$data['kontrol']->KODE_SEMESTER,$data['kontrol']->KODE_TAHUN);

          $data['page'] = 'entri_krs';

        }

        }
        else if($mhs == NULL)
        {
          $data['kontrol'] = NULL;
          $data['error'] = "Error: Maaf, data tidak dapat ditampilkan. Mungkin ada gangguan teknis. Silakan menghubungi bagian terkait.";
          $data['page'] = 'isi_krs_error';
        }

        $this->load->view($this->_template,$data);
	}

    function deleteKrs($id_mk_tersedia)
	{
        $ok = $this->akad_model->dataMatakuliahTersediaById($id_mk_tersedia);
        $krsSudahMasukEnrollments = $this->akad_model->cekKrsEnrollments($id_mk_tersedia,$this->session->userdata('siamunp_user'));

        if(!$ok)
        {
          $data['error'] = "Error: Maaf terjadi kesalahan dalam pemrosesan data.";
          $data['page'] = 'isi_krs_error';
          $data['kontrol'] = NULL;
          $this->load->view($this->_template,$data);
        }
        if($krsSudahMasukEnrollments)
        {
          $data['error'] = "Error: Matakuliah yang Anda pilih tidak bisa dihapus. Silakan hubungi bagian Administrasi / Akademik untuk menghapusnya.";
          $data['page'] = 'isi_krs_error';
          $data['kontrol'] = NULL;
          $this->load->view($this->_template,$data);
        }
        else
        {
          $this->akad_model->dataDeleteKrs($this->session->userdata('siamunp_user'),$id_mk_tersedia);

          redirect('siam/entri-krs.html', 'refresh');
        }
    }

    function viewKrs()
	{
        if($this->input->post('semester') && $this->input->post('tahun'))
        {
          $cari = $this->akad_model->getViewKrsAll($this->session->userdata('siamunp_user'),$this->input->post('tahun'),$this->input->post('semester'));
          if($cari)
          $data['getDataKrs'] = $this->akad_model->getViewKrsAll($this->session->userdata('siamunp_user'),$this->input->post('tahun'),$this->input->post('semester'));
          else
          $data['getDataKrs'] = $this->akad_model->getViewKrsAllKonversi($this->session->userdata('siamunp_user'),$this->input->post('tahun'),$this->input->post('semester'));
          if($data['getDataKrs'])
          {
            $cekTemporaryEnrollmentsAdaYangBelumValidasi = $this->akad_model->cekTemporaryEnrollments($this->session->userdata('siamunp_user'),$this->input->post('tahun'),$this->input->post('semester'));

            if($cekTemporaryEnrollmentsAdaYangBelumValidasi == TRUE) // ada yg belum divalidasi Ref: kprs
            $data['getDataKrs'] = NULL;
          }

          $data['page'] = 'detail_krs';

          $data['info_semester'] = $this->input->post('semester');

          $data['info_tahun'] = $this->input->post('tahun');
        }
        else
        {
          $mhs = $this->akad_model->getDataMahasiswa($this->session->userdata('siamunp_user'));

          $data['kontrol'] = $this->system_model->getViewKontrolData($mhs->KODE_PRODI);

          $data['optionsSemester'] = $this->system_model->getSelectOptionsValueSemester();

          //$data['optionsTahun'] = $this->system_model->getSelectOptionsValueTahunAjaran();

          $data['page'] = 'lihat_krs';
        }

        $data['atts'] = array(
              'width'      => '950',
              'height'     => '768',
              'scrollbars' => 'yes',
              'status'     => 'yes',
              'resizable'  => 'yes',
              'screenx'    => '0',
              'screeny'    => '0'
            );

        $this->load->view($this->_template,$data);
	}

    function viewKhs()
	{
	    if($this->input->post('semester') && $this->input->post('tahun'))
        {
          $cari = $this->akad_model->getViewKhsAll($this->session->userdata('siamunp_user'),$this->input->post('tahun'),$this->input->post('semester'));
          if($cari)
          $data['getDataKhs'] = $this->akad_model->getViewKhsAll($this->session->userdata('siamunp_user'),$this->input->post('tahun'),$this->input->post('semester'));
          else
          $data['getDataKhs'] = $this->akad_model->getViewKhsAllKonversi($this->session->userdata('siamunp_user'),$this->input->post('tahun'),$this->input->post('semester'));
          //$data['getDataKhs'] = $this->akad_model->getViewKhsAll($this->session->userdata('siamunp_user'),$this->input->post('tahun'),$this->input->post('semester'));
          if($data['getDataKhs'])
          {
            $cekTemporaryEnrollmentsAdaYangBelumValidasi = $this->akad_model->cekTemporaryEnrollments($this->session->userdata('siamunp_user'),$this->input->post('tahun'),$this->input->post('semester'));

            if($cekTemporaryEnrollmentsAdaYangBelumValidasi == TRUE) // ada yg belum divalidasi Ref: kprs
            $data['getDataKhs'] = NULL;
          }

          $data['getDataIpk'] = $this->akad_model->getViewKhsAllByNim($this->session->userdata('siamunp_user'));

          $data['tahun'] = $this->input->post('tahun');

          $data['semester'] = $this->input->post('semester');

          $data['page'] = 'detail_khs';
        }
        else
        {
          $mhs = $this->akad_model->getDataMahasiswa($this->session->userdata('siamunp_user'));

          $data['kontrol'] = $this->system_model->getViewKontrolData($mhs->KODE_PRODI);

          $data['optionsSemester'] = $this->system_model->getSelectOptionsValueSemester();

          //$data['optionsTahun'] = $this->system_model->getSelectOptionsValueTahunAjaran();

          $data['page'] = 'lihat_khs';
        }

        $data['atts'] = array(
              'width'      => '950',
              'height'     => '768',
              'scrollbars' => 'yes',
              'status'     => 'yes',
              'resizable'  => 'yes',
              'screenx'    => '0',
              'screeny'    => '0'
            );

        $this->load->view($this->_template,$data);
	}

    function printKhs($thn,$smt)
    {
        $data['dataMhs'] = $this->akad_model->getDetailDataMahasiswa($this->session->userdata('siamunp_user'));
		$data['prodi'] = $this->session->userdata('siamunp_user');
		$data['dataprodi'] = $this->akad_model->getProdiDetail($data['dataMhs']->KODE_PRODI);
        $data['datapejabat'] = $this->akad_model->getPejabat();
        $trap = $this->akad_model->getViewKhsAll($this->session->userdata('siamunp_user'),$thn,$smt);
        if($trap){
            $data['khs'] = $this->akad_model->getViewKhsAll($this->session->userdata('siamunp_user'),$thn,$smt);
        }
        else
        {
          $data['khs'] = $this->akad_model->getViewKhsAllKonversi($this->session->userdata('siamunp_user'),$thn,$smt);
        }
        //$data['khs'] = $this->akad_model->getViewKhsAll($this->session->userdata('siamunp_user'),$thn,$smt);
        $data['info_semester'] = $smt;

        $data['info_tahun'] = $thn;

        $data['title'] = "KHS-".$this->session->userdata('siamunp_user');
        $this->load->view('print_khs',$data);
    }

    function printKrs($thn,$smt)
    {
        //$viewKontrolData = $this->system_model->getViewKontrolData();
        //$data['dataMhs'] = $this->sirak_akad_model->getViewKrsAll($nim,$viewKontrolData->KODE_SEMESTER,$viewKontrolData->KODE_TAHUN,'single');
        $data['dataMhs'] = $this->akad_model->getDetailDataMahasiswa($this->session->userdata('siamunp_user'));
        $trap = $this->akad_model->getViewKrsAll($this->session->userdata('siamunp_user'),$thn,$smt);
        if($trap){
            $data['krs'] = $this->akad_model->getViewKrsAll($this->session->userdata('siamunp_user'),$thn,$smt);
        }
        else
        {
          $data['krs'] = $this->akad_model->getViewKrsAllKonversi($this->session->userdata('siamunp_user'),$thn,$smt);
        }
        $data['pa'] = $this->akad_model->getDetailDataMahasiswa($this->session->userdata('siamunp_user'));
        //$data['prodi'] = $nim;
		$data['dataprodi'] = $this->akad_model->getProdiDetail($data['dataMhs']->KODE_PRODI);
        if($smt == 1){

              $kode_semester = 2;

              $tahun = $thn - 1;
            }
            else if($smt == 2){

              $kode_semester = 1;

              $tahun = $thn;
            }
        $trap2 = $this->akad_model->getViewKhsAll($this->session->userdata('siamunp_user'),$tahun,$kode_semester);
        if($trap2){
            $data['getDataKhs'] = $this->akad_model->getViewKhsAll($this->session->userdata('siamunp_user'),$tahun,$kode_semester);
        }
        else
        {
          $data['getDataKhs'] = $this->akad_model->getViewKhsAllKonversi($this->session->userdata('siamunp_user'),$tahun,$kode_semester);
        }

        $data['info_semester'] = $smt;

        $data['info_tahun'] = $thn;

        $data['title'] = "KRS-".$this->session->userdata('siamunp_user');
        $this->load->view('print_krs',$data);
    }

    function viewTranskrip()
	{
	    $data['getDataTranskrip'] = $this->akad_model->getViewKhsAllByNim($this->session->userdata('siamunp_user'));
        if($data['getDataTranskrip'])
        {
            $cekTemporaryEnrollmentsAdaYangBelumValidasi = $this->akad_model->cekTemporaryEnrollments($this->session->userdata('siamunp_user'),$this->session->userdata('system_tahun_ajaran'),$this->session->userdata('system_semester'));

            if($cekTemporaryEnrollmentsAdaYangBelumValidasi == TRUE) // ada yg belum divalidasi Ref: kprs
              $data['getDataTranskrip'] = NULL;
        }

        $data['getDataTranskripDistinct'] = $this->akad_model->getViewKhsAllDistinct($this->session->userdata('siamunp_user'));

        $data['page'] = 'transkrip_khs';

        $this->load->view($this->_template,$data);
	}

    function viewPresensi()
	{
        $data['mhs'] = $this->akad_model->getDataMahasiswa($this->session->userdata('siamunp_user'));

        if ( ($data['mhs'] != NULL)){

          $data['kontrol'] = $this->system_model->getViewKontrolData($data['mhs']->KODE_PRODI);

          $data['daftarMkPresensi'] = $this->akad_model->getDaftarMkPresensi($data['kontrol']->KODE_SEMESTER, $data['kontrol']->KODE_TAHUN, $this->session->userdata('siamunp_user'));
          if($data['daftarMkPresensi'])
          {
              $cekTemporaryEnrollmentsAdaYangBelumValidasi = $this->akad_model->cekTemporaryEnrollments($this->session->userdata('siamunp_user'),$this->session->userdata('system_tahun_ajaran'),$this->session->userdata('system_semester'));

              if($cekTemporaryEnrollmentsAdaYangBelumValidasi == TRUE) // ada yg belum divalidasi Ref: kprs
                $data['daftarMkPresensi'] = NULL;
          }

        }
        else{

           $data['daftarMkPresensi'] = NULL;

           $data['mhs'] = NULL;

        }

        $data['page'] = 'lihat_presensi';

        $this->load->view($this->_template,$data);
	}

    function detailPresensi()
	{
	    $data['presensiData'] = $this->akad_model->getPresensiData($this->input->post('tahun'),$this->input->post('semester'),$this->input->post('mk'));

        $data['presensiDataNim'] = $this->akad_model->getPresensiDataNim($this->session->userdata('siamunp_user'),$this->input->post('tahun'),$this->input->post('semester'),$this->input->post('mk'));

        $data['page'] = 'detail_presensi';

        $this->load->view($this->_template,$data);
	}

    function detailKeuangan()
    {
        $data['dataNeraca'] = $this->akad_model->getNeraca($this->session->userdata('siamunp_user'));
    }

    function printJadwalKuliah()
    {
      $viewKontrolData = $this->system_model->getViewKontrolData($this->session->userdata('siamunp_user_prodi'));
      $data['title'] = "Jadwal Kuliah - Semester ".ucwords(strtolower($viewKontrolData->SEMESTER))." ".$viewKontrolData->TAHUN_AJARAN;

      $data['page'] = 'print_jadwal_kuliah';

      $data['hari'] = $this->system_model->getValueHari();

      $data['institusi'] = $this->akad_model->seeFakultas($this->session->userdata('siamunp_user_prodi'));

      $this->load->view($data['page'],$data);
    }

}

